package d4;

public class Student implements Comparable<Student>{
    public String name;
    public int age;

    //构造方法
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    public int compareTo(Student o){
        Student s = (Student) o;
        if(this.age>s.age){
            return -1;
        }else if(this.age<s.age){
            return 1;
        }else{
            return 0;
        }
        //return s.age-this.age;//根据年龄降序排列
        //return s.name.compareTo(this.name);//根据字母顺序降序排列
    }
    //模拟冒泡排序
    public static void sort(Comparable[] array){
        for(int i = 0; i< array.length; i++){
            for(int j = 0;j< array.length-i-1;j++){
                Comparable temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
}
